package com.sbj.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sbj.dto.req.*;
import com.sbj.dto.res.*;
import com.sbj.dto.utils.JWTUtils;
import com.sbj.service.*;
import com.zmh.framework.util.Result;
import lombok.AllArgsConstructor;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/12 10:40
 */
@RestController
@RequestMapping(value = "/member_order")
@AllArgsConstructor
public class MemberOrderController {

    private final IBuyGoodsService buyGoodsService;
    /*
    抢购订单
     */
    @PostMapping(value = "/submit_buy_order")
    public Result<String> submitBuyOrder(@RequestBody BuyReq req){
        buyGoodsService.submitBuyOrder(req);
        return Result.successResult();
    }

    private final IMemberStoreService memberStoreService;

    /**
     * 商城订单
     * @param req
     * @return
     */
    @PostMapping(value = "/shop_order_lists")
    public Result<Page<ShopOrderRes>>  shopOrderLists(@RequestBody ShopOrderReq req){
        return Result.successResult( orderService.shopOrderLists(req));
    }

    /**
     * 更新订单
     * @param req
     * @return
     */
    @PutMapping(value = "/update_shop_order")
    public Result<String> updateShopOrder(@RequestBody ShopOrderReq req){
        orderService.updateShopOrder(req);
        return Result.successResult();
    }

    /**
     * 订单详情
     * @param orderId
     * @return
     */
    @GetMapping(value = "/find_order_item/{orderId}")
    public Result<OrderItemRes> findOrderItem(@PathVariable Integer orderId){
        return Result.successResult(orderService.findOrderItem(orderId));
    }

    private final OrderKdService orderKdService;

    /**
     * 查看快递信息
     * @param orderId
     * @return
     */
    @GetMapping(value = "/kd_info/{orderId}")
    public Result<Map<String,Object>> kdInfo(@PathVariable Integer orderId){

        return Result.successResult(orderKdService.kdInfo(orderId));
    }

    /**
     * 订单支付
     * @param req
     * @return
     */
    @PostMapping(value = "/buy_order_pay")
    public Result<String> buyOrderPay(@RequestBody BuyReq req){
        memberStoreService.buyOrderPay(req);
        return Result.successResult();
    }

    /**
     * 取消订单
     * @param storeId
     * @return
     */
    @GetMapping(value = "/cancel_buy_order/{storeId}")
    public Result<String> cancelBuyOrder(@PathVariable Integer storeId){
        buyGoodsService.cancelBuyOrder(storeId);
        return Result.successResult();
    }



    /**
     * 获取转拍信息
     * @param id
     * @return
     */
    @GetMapping(value = "/get_transfer_ration/{id}")
    public Result<TransferRationRes>  getTransferRatio(@PathVariable Integer id){
        return Result.successResult(memberStoreService.getTransferRatio(id));
    }


    /**
     * 支付订单
     * @param id
     * @return
     */
    @GetMapping(value = "/pay_order/{id}")
    public Result<MemberStoreRes>  payOrder(@PathVariable Integer id){

        return Result.successResult(buyGoodsService.payOrder(id));
    }

    private final OrderService orderService;

    /**
     * 商品转jwt
     * @param req
     * @return
     */
    @PostMapping(value = "/goodsToJwt")
    public Result<String> goodsToJwt(@RequestBody List<OrderGoodsReq> req){
        Map<String,Object> map= MapUtil.newHashMap();
        map.put("sign",req);
        String sign = JWTUtils.sign(map);
        return Result.successResult(sign);
    }

    private final GoodsService goodsService;

    /**
     * 商品解密
     * @param req
     * @return
     */
    @PostMapping(value = "/jwtToGoods")
    public Result<List<OrderGoodsRes>> jwtToGoods(@RequestBody SubmitOrderReq req){

        return Result.successResult(goodsService.jwtToGoods(req));
    }

    /**
     * 商城订单
     * @return
     */
    @PostMapping(value = "/submit_order")
    public Result<String> submitOrder(@RequestBody SubmitOrderReq req){
        orderService.submitOrder(req);
        return Result.successResult();
    }

    @PostMapping(value = "/resubmit_order")
    public Result<String> resubmitOrder(@RequestBody OrderReq req){
        orderService.resubmitOrder(req);
        return Result.successResult();
    }

    /**
     * 转拍商品
     * @return
     */
    @PostMapping(value = "/transfer_goods")
    public Result<String> transferGoods(@RequestBody TransferGoodReq req){
        memberStoreService.transferGoods(req);
        return Result.successResult();
    }


    @GetMapping(value = "/get_buy_order_info/{id}")
    public Result<MemberStoreRes> memberStoreInfo(@PathVariable Integer id){

        return Result.successResult(memberStoreService.memberStoreInfo(id));
    }

}
